package com.obrock;

import android.app.DialogFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

/**
 * Created with IntelliJ IDEA.
 * User: Дмитрий Г. Кузнецов
 * Date: 02.11.12
 * Time: 11:06
 * To change this template use File | Settings | File Templates.
 */

public class EditAnalytics extends DialogFragment {

    EditText name;
    EditText remark;
    Button save;
    Button delete;
    Button cancel;

    Database db;
    Analytics analytics = null;
    boolean created = false;

    public interface EditAnalyticsListener {
        boolean onUpdateAnalytics(Analytics analytics);
        boolean onAppendAnalytics(Analytics analytics);
        boolean onDeleteAnalytics(Analytics analytics);
    }

    public EditAnalytics(Database db, long parentID, Analytics analytics) {
        this.db = db;
        if (analytics == null) {
            this.analytics = new Analytics(db);
            this.analytics.parentID = parentID;
            created = true;
        } else {
            this.analytics = analytics;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.edit_analytics, container);

        name = (EditText) view.findViewById(R.id.name);
        name.setText(analytics.name);

        remark = (EditText) view.findViewById(R.id.remark);
        remark.setText(analytics.remark);

        save = (Button) view.findViewById(R.id.save);
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                analytics.name = name.getText().toString();
                analytics.remark = remark.getText().toString();
                EditAnalyticsListener activity = (EditAnalyticsListener) getActivity();
                boolean result = false;
                if (created) {
                    result = activity.onAppendAnalytics(analytics);
                } else {
                    result = activity.onUpdateAnalytics(analytics);
                }
                if (result) {
                    EditAnalytics.this.dismiss();
                }
            }
        });

        delete = (Button) view.findViewById(R.id.delete);
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditAnalyticsListener activity = (EditAnalyticsListener) getActivity();
                if (activity.onDeleteAnalytics(analytics)) {
                    EditAnalytics.this.dismiss();
                }
            }
        });

        cancel = (Button) view.findViewById(R.id.cancel);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditAnalytics.this.dismiss();
            }
        });


        getDialog().setTitle(getResources().getString(R.string.analytics));

        return view;
    }
}
